package com.neuedu.elm.dao.Impl;

import com.neuedu.elm.dao.IFoodDao;
import com.neuedu.elm.po.Food;
import com.neuedu.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.SocketHandler;

public class FoodDaoImpl implements IFoodDao {

    @Override
    public List<Food> findAllFood(int businessId) {
        List<Food> list = new ArrayList<>();

        PreparedStatement ps = null;
        Connection connection = DBUtil.getConnection();
        ResultSet rs = null;
        String sql = "select foodId,foodName,foodExplain,foodPrice,businessId from food where businessId=?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, businessId);
            rs = ps.executeQuery();
            while (rs.next()) {
                Food food = new Food();
                food.setFoodId(rs.getInt(1));
                food.setFoodName(rs.getString(2));
                food.setFoodExplain(rs.getString(3));
                food.setFoodPrice(rs.getFloat(4));
                food.setBusinessId(rs.getInt(5));
                list.add(food);
                System.out.println(food.toString1());
            }
//            for (Object o : list
//            ) {
//                System.out.println(o);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(ps, connection);

        }

        return null;
    }

    @Override
    public int addFood(Food food) {
        System.out.println(food);
        List<Food> list = new ArrayList<>();

        PreparedStatement ps = null;
        Connection connection = DBUtil.getConnection();
        int rs = 0;
        String sql = " INSERT INTO food (foodName, foodExplain, foodPrice, businessId) VALUES (?,?,?,?)";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, food.getFoodName());
            ps.setString(2, food.getFoodExplain());
            ps.setFloat(3, food.getFoodPrice());
            ps.setInt(4, food.getBusinessId());
            rs = ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(ps, connection);

        }

        return rs;
    }

    @Override
    public int updateFood(Food food, String name) {
//        System.out.println(food);这是我的测试数据
        List<Object> params = new ArrayList<>();
        PreparedStatement ps = null;
        Connection connection = DBUtil.getConnection();
        String sql = "update food set businessId=?";
        params.add(food.getBusinessId());
        if (food.getFoodName() != null && !food.getFoodName().isEmpty()) {
            sql = sql + "  ,foodName=? ";
            params.add(food.getFoodName());
        }
        if (food.getFoodExplain() != null && !food.getFoodExplain().isEmpty()) {
            sql = sql + " ,foodExplain=? ";
            params.add(food.getFoodExplain());
        }
        if (food.getFoodPrice() != null) {
            sql = sql + " ,foodPrice=? ";
            params.add(food.getFoodPrice());
        }
        sql = sql + " where foodName=? and businessId=?";

        params.add(name);
        params.add(food.getBusinessId());
        int result = 0;
        try {
            ps = connection.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                ps.setObject(i + 1, params.get(i));
            }
            //    System.out.println(sql);这是我的测试数据
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，修改失败");
        } finally {
            DBUtil.close(ps, connection);
        }
        return result;
    }

    @Override
    public int deleteFood(Food food) {
        PreparedStatement ps = null;
        int result = 0;
        Connection connection = DBUtil.getConnection();
        String sql = "delete from food where foodName=?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, food.getFoodName());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，删除失败");
        } finally {
            DBUtil.close(ps, connection);
        }

        return result;
    }
}
